using AppToolKit.Business;
using AppToolKit.Data;
using AppToolKit.Data.NHibernate;
using NHibernate;
using Spring.Context.Support;
using Spring.Objects.Factory.Config;
using Spring.Objects.Factory.Support;
using System;
using System.Reflection;
namespace AppToolKit.Core.Context.Spring.AutoConstruction.Builder
{
	public class CustomImplBusinessServerBuilder : ServerBuilderBase
	{
		private CustomImplBusinessSupport support_ = new CustomImplBusinessSupport();
		protected override string GetBusinessName(Type businessType)
		{
			return BusinessNameSymbol.GetBusinessName(businessType.FullName);
		}
		protected override void RegisterBusiness(Type businessType)
		{
			Type entityType = this.support_.GetEntityType(businessType);
			string businessName = this.GetBusinessName(businessType);
			DefaultListableObjectFactory defaultListableObjectFactory = (DefaultListableObjectFactory)((XmlApplicationContext)base.ApplicationContext).ObjectFactory;
			if (base.ConstructionLocation.ContainImplement(businessType))
			{
				Type implementType = base.ConstructionLocation.GetImplementType(businessType);
				PropertyInfo property = implementType.GetProperty("Dao", BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
				if (property == null)
				{
					property = implementType.GetProperty("Dao", BindingFlags.Instance | BindingFlags.Public);
				}
				Type type;
				string text;
				if (base.ConstructionLocation.ContainImplement(property.PropertyType))
				{
					type = base.ConstructionLocation.GetImplementType(property.PropertyType);
					text = type.FullName + "_dao";
				}
				else
				{
					if (!property.PropertyType.IsGenericType || !TypeHelper.IsGeneric(property.PropertyType, typeof(IDataAccessObject<>)))
					{
						throw new AutoConstructionException(string.Format("自动构造找不到相关的业务实现{0}", businessType));
					}
					Type typeFromHandle = typeof(DefaultDataAccess<>);
					Type type2 = typeFromHandle.MakeGenericType(new Type[]
					{
						entityType
					});
					type = type2;
					text = entityType.FullName + "_dao";
				}
				RootObjectDefinition rootObjectDefinition = new RootObjectDefinition(type);
				rootObjectDefinition.IsSingleton=false;
				rootObjectDefinition.PropertyValues.Add("NativeDatabase", new RuntimeObjectReference(this.helper_.GetObjectName(typeof(IDbProcessor))));
				rootObjectDefinition.PropertyValues.Add("SessionFactory", new RuntimeObjectReference(this.helper_.GetObjectName(typeof(ISessionFactory))));
				defaultListableObjectFactory.RegisterObjectDefinition(text, rootObjectDefinition);
				RootObjectDefinition rootObjectDefinition2 = new RootObjectDefinition(implementType);
                rootObjectDefinition2.IsSingleton = false;
                rootObjectDefinition2.PropertyValues.Add("Dao", new RuntimeObjectReference(text));
                rootObjectDefinition2.PropertyValues.Add("Support", new RuntimeObjectReference(this.helper_.GetObjectName(typeof(BusinessSupport))));
				System.Type typeFromHandle2 = typeof(IEntityFactory<>);
                System.Type type3 = typeFromHandle2.MakeGenericType(new Type[]
				{
					entityType
				});
				if (base.ConstructionLocation.ContainImplement(entityType))
				{
					Type implementType2 = base.ConstructionLocation.GetImplementType(entityType);
					RootObjectDefinition rootObjectDefinition3 = new RootObjectDefinition(implementType2);
					rootObjectDefinition3.IsSingleton=false;
					string text2 = entityType.FullName + "_factory";
					defaultListableObjectFactory.RegisterObjectDefinition(text2, rootObjectDefinition3);
					rootObjectDefinition2.PropertyValues.Add("Factory", new RuntimeObjectReference(text2));
				}
				defaultListableObjectFactory.RegisterObjectDefinition(businessName, rootObjectDefinition2);
              
				return;
			}
			throw new AutoConstructionException(string.Format("自动构造找不到相关的业务实现{0}", businessType));
		}
		public override bool IsSupport(Type type)
		{
			return this.support_.IsSupport(type);
		}
	}
}
